package com.huaao.micropolice.core.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.huaao.micropolice.common.security.Digests;
import com.huaao.micropolice.common.util.Encodes;
import com.huaao.micropolice.core.common.page.GridPage;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Parameter;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.repository.system.UserDao;
import com.huaao.micropolice.core.security.CredentialsDigest;
import com.huaao.micropolice.core.service.system.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService{
	
	private static final int SALT_SIZE = 8;
	private static final String RAW_PASSWORD = "123456";
	@Autowired
	private UserDao userDao;
	@Autowired
	private CredentialsDigest credentialsDigest;
	
	@Override
	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}

	@Override
	public String save(User user)  throws Exception{
		if(userDao.findByUsername(user.getUsername()) != null){
			throw new Exception("用户名["+user.getUsername()+"]已经存在，");
		}
		user.setRawPassword(RAW_PASSWORD);
		entryptPassword(user);
		return userDao.save(user);
	}

	@Override
	public User findByUserid(String uid) {		
		return userDao.findByUserId(uid);
	}

	@Override
	public void updateUser(User user)  throws Exception{
		User u = userDao.load(user.getId());
		u.setCode(user.getCode());
		u.setDeptId(user.getDeptId());
		userDao.update(u);
	}

	@Override
	public Page findByUserLike(String name,GridPage gridPage) {
		Page page = new Page();
		Sort sort = new Sort();
		if(gridPage != null){
			page.setPageNum(gridPage.getPage());
			page.setPageSize(gridPage.getRows());
			if(!StringUtils.isEmpty(gridPage.getSidx()) ){
				sort.setName(gridPage.getSidx());
				sort.setType(gridPage.getSord());
			}
		}
		List<Parameter> params = new ArrayList<Parameter>();
		if(!StringUtils.isEmpty(name)){
			params.add(new Parameter("name_cn_"+name));
		}
		userDao.findByPage(User.class, params, sort, page);
		return  page;
 		
	}

	@Override
	public void del(String id)  throws Exception{
		userDao.deleteById(User.class, id);
		
	}

	@Override
	public void resetPassword(String[] ids) {
		for(String id : ids){
			User user = userDao.load(id);
			user.setRawPassword(RAW_PASSWORD);
			entryptPassword(user);
			userDao.update(user);
		}
	}

	@Override
	public List<User> findByDeptId(String id) {
		return userDao.findByDeptId(id);
	}

	@Override
	public List<User> findByJobsId(String id) {
		return userDao.findByJobsId(id);
	}

	@Override
	public void addUser(User user) {
		entryptPassword(user);
		userDao.save(user);
	}
	
	
	/**
	 * 对用户进行密码加密
	 * @param user
	 */
	private void entryptPassword(User user){
		byte[] saltBytes = Digests.generateSalt(SALT_SIZE);
		String salt = Encodes.encodeHex(saltBytes);
		user.setSalt(salt);
		String rawPass = user.getRawPassword();
		String encPass = credentialsDigest.digest(rawPass, saltBytes);
		user.setPassword(encPass);
	}
	
	/**
	 * 判断密码是否正确
	 * @param user 用户对象
	 * @param rawPassword 输入的密码
	 * @return
	 */
	public boolean checkPassword(User user , String rawPassword){
		User u = userDao.load(user.getId());
		return credentialsDigest.matches(u.getPassword(), rawPassword, u.getSaltBytes());
	}
}
